package com.srit.map.business.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srit.core.constant.SystemConstant;

public class CheckLoginFilter implements Filter {

	static Logger log = LoggerFactory.getLogger(CheckLoginFilter.class);
	private FilterConfig filterConfig;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse rep = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		if (session.getAttribute(SystemConstant.SESSION_USER) == null) {
			// 跳转登录页面
			String login = filterConfig.getInitParameter("LOGIN");
			// System.out.println(req.getServerPort()+"req.getContextPath()");
			rep.sendRedirect(req.getContextPath() + login);
			// ACE:以下内容注释暂时先不拦截
			// RequestDispatcher dispatcher =
			// request.getRequestDispatcher(login);
			// dispatcher.forward(request, response);
		} else {
			chain.doFilter(request, response);
		}
	}

	@Override
	public void destroy() {

	}

}
